-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here
--#region
local Util = require("lazyvim.util")

local function map(mode, lhs, rhs, opts)
  local keys = require("lazy.core.handler").handlers.keys
  if not keys.active[keys.parse({ lhs, mode = mode }).id] then
    opts = opts or {}
    opts.silent = opts.silent ~= false
    if opts.remap and not vim.g.vscode then
      opts.remap = nil
    end
    vim.keymap.set(mode, lhs, rhs, opts)
  end
end
vim.keymap.set("x", "s", "<Plug>VSurround")
vim.keymap.set("x", "S", "<Plug>VgSurround")
-- Markdowns
map("n", "md", "<cmd>InstantMarkdownPreview<cr>", { desc = "MarkdownPerview" })

map("n", "<C-p>", ":Telescope find_files<Cr>")
map("n", "<C-r>", ":Telescope projects<Cr>")
map("n", "<C-b>", ":Telescope buffers<Cr>")
map("n", "<C-f>", ":Telescope live_grep<Cr>")

-- lazygit
map("n", "<C-g>", function()
  Util.float_term({ "lazygit" }, { esc_esc = false, ctrl_hjkl = false })
end, { desc = "Lazygit (cwd)" })
-- bookmark
map("n", "bm", ":Telescope vim_bookmarks all<cr>")
map("n", "bmc", ":Telescope vim_bookmarks current_file<cr>")
-- Move to window using the <ctrl> hjkl keys
map("n", "sh", "<C-w>h", { desc = "Go to left window", remap = true })
map("n", "sj", "<C-w>j", { desc = "Go to lower window", remap = true })
map("n", "sk", "<C-w>k", { desc = "Go to upper window", remap = true })
map("n", "sl", "<C-w>l", { desc = "Go to right window", remap = true })
-- Resize with arrows
map("n", "<Up>", ":resize -2<CR>")
map("n", "<Down>", ":resize +2<CR>")
map("n", "<Left>", ":vertical resize -2<CR>")
map("n", "<Right>", ":vertical resize +2<CR>")
--split
map("n", "sv", "<C-w>v", { desc = "Go to left window", remap = true })
map("n", "ss", "<C-w>s", { desc = "Go to left window", remap = true })
map("n", "so", "<C-w>o", { desc = "Go to left window", remap = true })

-- floating terminal
local lazyterm = function()
  Util.float_term(nil, { cwd = Util.get_root() })
end
map("n", "<leader>ft", lazyterm, { desc = "Terminal (root dir)" })
map("n", "<leader>fT", function()
  Util.float_term()
end, { desc = "Terminal (cwd)" })
map("n", "<c-/>", lazyterm, { desc = "Terminal (root dir)" })
map("n", "<c-_>", lazyterm, { desc = "which_key_ignore" })
